// manager/pages/process/components/basicinfo/index.js
import config from '../../../../../utils/config';
import { request, uploadFile } from '../../../../../utils/request';
import util from '../../../../../common/util';
import WxValidate from '../../../../../common/WxValidate';
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
			type: Boolean,
			value: false
        },
        readonly: {
			type: Boolean,
			value: true
        },
        form: {
			type: Object,
			value: () => {
                return {}
            }
        },
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () { 
            
        },
        ready: function () {
            this.initValidate();
        },
        moved: function () { 
            //console.log("moved")
        },
        detached: function () { 
            //console.log("detached")
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        /** 验证表单是否通过 */
		validationForm(isToast = false){
			// 传入表单数据，调用验证方法
            if (!this.WxValidate.checkForm(this.data.form)) {
                if(isToast){
                    const error = this.WxValidate.errorList[0]
                    this.showModal(error)
                }
                return false
            }
			return true
		},
		/** 提交 */
		submit() {
			if(this.validationForm(true)){
                this.triggerEvent('save', this.data.form);
			}
        },
        // 表单验证
        initValidate() {
            // 验证字段的规则
            const rules = {
                locationName: {
                    required: true
                },
                locationAbbreviation: {
                    required: true
                },
                locationPic: {
                    required: true
                },
                locationCoordinate: {
                    required: true
                },
                locationLayers: {
                    required: true,
                    digits: true,
                    min: 1
                }
            }
            // 验证字段的提示信息，若不传则调用默认的信息
            const messages = {
                locationName: {
                    required: '请输入楼宇名称'
                },
                locationAbbreviation: {
                    required: '请输入楼宇简称'
                },
                locationPic: {
                    required: '请上传楼宇照片'
                },
                locationCoordinate: {
                    required: '请获取楼宇位置'
                },
                locationLayers: {
                    required: '请输入总楼层',
                    digits: '总楼层为数字',
                    min: '总楼层不小于1'
                }
            }
            // 创建实例对象
            this.WxValidate = new WxValidate(rules, messages)
        },
        showModal(error) {
            wx.showToast({
                title: error.msg,
                icon: 'none',
                duration: 2000
            })
        },
    }
})